#define NoAdminMode
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CanEditMultipleObjects]
[ExecuteInEditMode]
public class HMI_EditorBase : Editor
{
    #region Const
    public const string MiniWidgetPath = "Assets/ThirdParty/isletTools/UIWidgetAsset/miniWidget/";
    public const string WidgetType_AutoWidthButton = "AutoWidthButton";
    public const string WidgetType_Button = "Button";
    public const string WidgetType_Button_Image = "Button_Image";
    public const string WidgetType_Button_Text = "Button_Text";
    public const string WidgetType_HorizontalItem = "HorizontalItem";
    public const string WidgetType_IconHorizontalButton = "IconHorizontalButton";
    public const string WidgetType_IconVerticalButton = "IconVerticalButton";
    public const string WidgetType_SlideButton = "SlideButton";
    public const string WidgetType_Slider = "Slider";
    public const string WidgetType_TabGroupBtn = "TabGroupBtn";
    public const string WidgetType_ToggleSingle = "ToggleSingle";
    public const string WidgetType_ToggleSwitch = "ToggleSwitch";
    #endregion Const

    private bool commonFoldout = false;
    private HMI_ComponentBase mTargetBase;
    public virtual void Awake()
    {
        mTargetBase = target as HMI_ComponentBase;
    }

    public override void OnInspectorGUI()
    {
        commonFoldout = EditorToolsUtils.CreateFoldout("通用", DrawBase, commonFoldout);
    }

    private void DrawBase()
    {
#if AdminMode
        if (GUILayout.Button("复制（图片与UI预制体要在同一路径下）"))
        {
            var data = mTargetBase.GetDataBase();
            GUIUtility.systemCopyBuffer = JsonUtility.ToJson(data, true) + "|" + GetDataJson();
        }
        if (GUILayout.Button("粘贴"))
        {
            var json = GUIUtility.systemCopyBuffer.Split('|');
            HMI_ComponentBaseData data = JsonUtility.FromJson<HMI_ComponentBaseData>(json[0]);

            if (data.widgetType != mTargetBase.widgetType)
            {
                Debug.LogError("复制控件类型不匹配,复制失败！！！复制类型：" + data.widgetType + " 目标类型：" + mTargetBase.widgetType);
                return;
            }

            mTargetBase.SetDataBase(data);
            SetData(json[1]);
        }
        EditorGUILayout.Separator();
        if (string.IsNullOrEmpty(mTargetBase.widgetType))
        {
            mTargetBase.widgetType = mTargetBase.name;
            EditorUtility.SetDirty(mTargetBase);
        }
        mTargetBase.widgetType = EditorGUILayout.TextField("Type:", mTargetBase.widgetType);
#endif
        mTargetBase.RTLDontChange = EditorGUILayout.Toggle("RTL布局不变：", mTargetBase.RTLDontChange);
        mTargetBase.VoiceTitle = EditorGUILayout.TextField("语音标题：", mTargetBase.VoiceTitle);
        EditorGUILayout.Separator();
    }

    #region 序列化
    protected virtual string GetDataJson()
    {
        return "";
    }

    protected virtual void SetData(string json)
    {
       
    }
    #endregion 序列化

    #region 工具
    public static bool isFrameAniName(Sprite sp)
    {
        if (sp == null)
            return false;

        if (!sp.name.Contains("_"))
            return false;

        var list = sp.name.Split('_');
        if (int.TryParse(list[list.Length - 1], out var index))
            return true;

        return false;
    }

    public static GameObject LoadMiniWidget(Transform root, string name)
    {
        var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(MiniWidgetPath + name + ".prefab");
        var oldPos = (prefab.transform as RectTransform).anchoredPosition;
        var obj = GameObject.Instantiate(prefab);
        obj.transform.parent = root;
        (obj.transform as RectTransform).anchoredPosition = oldPos;
        obj.name = name;
        return obj;
    }
    #endregion 工具
}
